package com.hospital.warehouse.outstock.service.impl;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.util.DTO;
import com.hospital.util.JMyUtil;
import com.hospital.warehouse.drugstock.dao.DrugstockDao;
import com.hospital.warehouse.drugstock.entity.Drugstock;
import com.hospital.warehouse.drugstock.service.DrugstockService;
import com.hospital.warehouse.exception.WarehouseException;
import com.hospital.warehouse.instock.dao.InstockDao;
import com.hospital.warehouse.outstock.dao.OutstockDao;
import com.hospital.warehouse.outstock.entity.Outstock;
import com.hospital.warehouse.outstock.service.OutstockService;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Mr. YR
 * @since 2020-04-20
 */
@Service
public class OutstockServiceImpl extends ServiceImpl<OutstockDao, Outstock> implements OutstockService {

	@Autowired
	OutstockDao storehouseDao;
	@Autowired
	InstockDao instockDao;
	@Autowired
	DrugstockDao drugstockDao;
	@Autowired
	DrugstockService drugstockService;

	@Override
	public DTO paging(Outstock po, Integer page, Integer limit) throws ParseException {
//		System.out.println(po);

		if (JMyUtil.notEmpty(po.getOs_outtime_interval())) {
			String[] dates = po.getOs_outtime_interval().split(" ~ ");
			po.setOs_outtime_min(JMyUtil.formatStringToDate_yMdHms(dates[0]));
			po.setOs_outtime_max(JMyUtil.formatStringToDate_yMdHms(dates[1]));
		}

		Page<Outstock> pageinfo = new Page<>(page, limit);

		IPage<Outstock> iPage = storehouseDao.paging(po, pageinfo, null);
		List<Outstock> list = iPage.getRecords();

		DTO dto = new DTO();
		dto.setCount(iPage.getTotal());
		dto.setData(list);

		if (list.size() <= 0) {
			dto.setMsg("没有记录");
		}

		return dto;
	}

	@Override
	public Outstock queryById(Integer id) {
		Outstock po = storehouseDao.queryById(id);
		return po;
	}

	@Transactional
	@Override
	public void saveOne(Outstock po, HttpSession session) {
//		System.out.println(po);

		// 查询出库数量是否大于剩余数量
		querySurplusNum(po);

		// 库存减少数量，传入Outstock中有入库单id和数量
		drugstockDao.reduceNum(po);

		// 新增一条出库记录
		po.insert();
	}

	@Transactional
	@Override
	public void updateOne(Outstock newpo) {
		// 查出旧的出库单，重点需要数量、入库单批次信息
		Outstock oldpo = newpo.selectById();
//		System.out.println("oldpo " + oldpo.getOs_id() + " " + oldpo.getOs_is_id() + " " + oldpo.getOs_num());
//		System.out.println("newpo " + newpo.getOs_id() + " " + newpo.getOs_is_id() + " " + newpo.getOs_num());

		// sql语句里是-符号，乘-1表示加上
		Integer oldnum = oldpo.getOs_num();
		oldpo.setOs_num(oldnum * -1);
		// 库存旧批次加回数量，传入Outstock中有入库单id和数量
		drugstockDao.reduceNum(oldpo);

		// 查询新出库数量是否大于剩余数量
		querySurplusNum(newpo);
		// 库存新批次减少数量
		drugstockDao.reduceNum(newpo);

		newpo.updateById();
	}

	// 查询 出库数量 是否大于 剩余数量
	private void querySurplusNum(Outstock po) {
		Integer os_num = po.getOs_num();
		if (os_num <= 0) {
			throw new WarehouseException("出库失败：出库数量必须大于等于1！");
		}
		Integer is_id = po.getOs_is_id();
		// 根据入库单id，查询出库单已出库总数，取别名叫sum。自动添加假删除标识
		QueryWrapper<Drugstock> wrapper = new QueryWrapper<Drugstock>().select("sum(ds_num) sum").eq("ds_is_id", is_id);
		Map<String, Object> map = drugstockService.getMap(wrapper);
		// 没有map，说明没有库存项，说明没有批次
		if (map == null) {
			throw new WarehouseException("没有这项批次");
		}
		Integer ds_num = Integer.valueOf(map.get("sum").toString());
		// 如果 出库数量 大于 同批次剩余数，抛出异常
		if (os_num > ds_num) {
			throw new WarehouseException("出库失败：同批次 库存不足！");
		}
	}

}
